package com.beamfield.controller.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.enums.QrcodeEnum;
import com.beamfield.model.ManagerInfo;
import com.beamfield.model.NormalOperator;
import com.beamfield.model.SpecialOperator;
import com.beamfield.model.SystemDictionaryData;
import com.beamfield.modelVo.SystemDictionaryDataVo;
import com.beamfield.service.api.ApiService;
import com.beamfield.service.manage.sys.FileService;
import com.beamfield.service.manage.operator.ManagerInfoService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class ApiController {
	
	@Resource
	ApiService apiService;
	
	@Resource
	ManagerInfoService managerInfoService;
	
	@Resource
	FileService fileService;
	
	/**
	 * 二维码接口
	 * @param request
	 * @return
	 */
	@RequestMapping("/getInfos")
	@ResponseBody
	public ApiResult getInfos(HttpServletRequest request){
		// beam ,data, type
		String beam = request.getParameter("beam");
		String data = request.getParameter("data");
		String type = request.getParameter("type");
		
		if(CommonUtils.isEmptyString(beam)){
			return new ApiResult("1", "梁场id不能为空！");
		}
		if(CommonUtils.isEmptyString(data)){
			return new ApiResult("1", "数据id不能为空！");
		}
		if(CommonUtils.isEmptyString(type)){
			return new ApiResult("1", "类型不能为空！");
		}
		
		//管理人员
		if(QrcodeEnum.USER_MANAGER.getType().equals(type)){
			
			ManagerInfo managerInfo = apiService.getManagerInfoById(CommonUtils.parseLong(data, 0l));
			if(managerInfo == null){
				return new ApiResult("1", "数据异常，查不到数据！");
			}
			
			if(managerInfo.getBeam_id() != CommonUtils.parseLong(beam, 0l)){
				return new ApiResult("1", "梁场不匹配！无查看权限！");
			}
			
			if(managerInfo.getAvatar() != null){
				managerInfo.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(managerInfo.getAvatar())));
			}
			if(managerInfo.getQrcode() != null){
				managerInfo.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(managerInfo.getQrcode())));
			}
			
			SystemDictionaryData sex = managerInfoService.selectByCode(managerInfo.getGender(),managerInfo.getBeam_id());
			
			if(sex != null){
				managerInfo.setGender(sex.getName());
			}
			
			SystemDictionaryData workdept = managerInfoService.selectByCode(managerInfo.getWorkdept(),managerInfo.getBeam_id());
			
			if(workdept != null){
				managerInfo.setWorkdept(workdept.getName());
			}
			
			SystemDictionaryData volk = managerInfoService.selectByCode(managerInfo.getNation(),managerInfo.getBeam_id());
			
			if(volk != null){
				managerInfo.setNation(volk.getName());
			}
			
			SystemDictionaryData political = managerInfoService.selectByCode(managerInfo.getPolitical(),managerInfo.getBeam_id());
			
			if(political != null){
				managerInfo.setPolitical(political.getName());
			}
			
			SystemDictionaryData health = managerInfoService.selectByCode(managerInfo.getHealth(),managerInfo.getBeam_id());
			
			if(health != null){
				managerInfo.setHealth(health.getName());
			}
			
			SystemDictionaryData education = managerInfoService.selectByCode(managerInfo.getEducation(),managerInfo.getBeam_id());
			
			if(education != null){
				managerInfo.setEducation(education.getName());
			}
			List<Map<String, String>> managerTrain = apiService.getManagerTrain(managerInfo.getEcode(), beam);
			Map<String,Object> map = new HashMap<>();
			map.put("managerTrain",managerTrain);
			map.put("managerInfo",managerInfo);
			return new ApiResult(map);
		}
		
		//特殊作业人员
		if(QrcodeEnum.USER_SPECIAL.getType().equals(type)){
			
			SpecialOperator specialOperator = apiService.getSpecialOperatorById(CommonUtils.parseLong(data, 0l));
			if(specialOperator == null){
				return new ApiResult("1", "数据异常，查不到数据！");
			}
			
			if(specialOperator.getBeam_id() != CommonUtils.parseLong(beam, 0l)){
				return new ApiResult("1", "梁场不匹配！无查看权限！");
			}
			
			if(specialOperator.getAvatar() != null){
				specialOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(specialOperator.getAvatar())));
			}
			
			SystemDictionaryData sex = managerInfoService.selectByCode(specialOperator.getGender(),specialOperator.getBeam_id());
			if(sex != null){
				specialOperator.setGender(sex.getName());
			}
			
			SystemDictionaryData workdept = managerInfoService.selectByCode(specialOperator.getWorkdept(),specialOperator.getBeam_id());
			if(workdept != null){
				specialOperator.setWorkdept(workdept.getName());
			}
			
			SystemDictionaryData group = managerInfoService.selectByCode(specialOperator.getGroup(),specialOperator.getBeam_id());
			if(group != null){
				specialOperator.setGroup(group.getName());
			}
			
			SystemDictionaryData work_type = managerInfoService.selectByCode(specialOperator.getWork_type(),specialOperator.getBeam_id());
			if(work_type != null){
				specialOperator.setWork_type(work_type.getName());
			}
			
			if(specialOperator.getQrcode() != null){
				specialOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(specialOperator.getQrcode())));
			}
			List<Map<String, String>> managerTrain = apiService.getManagerTrain(specialOperator.getEcode(), beam);
			Map<String,Object> map = new HashMap<>();
			map.put("managerTrain",managerTrain);
			map.put("specialOperator",specialOperator);
			return new ApiResult(map);
		}
		//普通人员
		if(QrcodeEnum.USER_NORMAL.getType().equals(type)){
			
			NormalOperator normalOperator = apiService.getNormalOperatorById(CommonUtils.parseLong(data, 0l));
			if(normalOperator == null){
				return new ApiResult("1", "数据异常，查不到数据！");
			}
			
			if(normalOperator.getBeam_id() != CommonUtils.parseLong(beam, 0l)){
				return new ApiResult("1", "梁场不匹配！无查看权限！");
			}
			
			if(normalOperator.getAvatar() != null){
				normalOperator.setPic_url(CommonUtils.getFielUrl(fileService.getFileById(normalOperator.getAvatar())));
			}

			SystemDictionaryData sex = managerInfoService.selectByCode(normalOperator.getGender(),normalOperator.getBeam_id());
			if(sex != null){
				normalOperator.setGender(sex.getName());
			}
			
			SystemDictionaryData workdept = managerInfoService.selectByCode(normalOperator.getWorkdept(),normalOperator.getBeam_id());
			if(workdept != null){
				normalOperator.setWorkdept(workdept.getName());
			}
			
			SystemDictionaryData group = managerInfoService.selectByCode(normalOperator.getGroup(),normalOperator.getBeam_id());
			if(group != null){
				normalOperator.setGroup(group.getName());
			}
			
			if(normalOperator.getQrcode() != null){
				normalOperator.setQr_url(CommonUtils.getQrUrl(fileService.getFileById(normalOperator.getQrcode())));
			}

			List<Map<String, String>> managerTrain = apiService.getManagerTrain(normalOperator.getEcode(), beam);
			Map<String,Object> map = new HashMap<>();
			map.put("managerTrain",managerTrain);
			map.put("normalOperator",normalOperator);
			return new ApiResult(map);
		}
		
		return new ApiResult("1", "类型错误！");
		
	}
	
}
